<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
      <el-row>
        <el-form-item label="报名活动" prop="fromActivityId">
          <el-select v-model="queryParams.fromActivityId" placeholder="请选择报名活动" style="width:100%;">
            <el-option
              v-for="activity in activities"
              :key="activity.id"
              :label="activity.name"
              :value="activity.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="中奖活动" prop="awardActivityId">
          <el-select v-model="queryParams.awardActivityId" placeholder="请选择中奖活动" style="width:100%;">
            <el-option
              v-for="activity in activities"
              :key="activity.id"
              :label="activity.name"
              :value="activity.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户昵称" prop="nickname">
          <el-input v-model="queryParams.nickname" placeholder="请输入用户昵称" clearable size="small" @keyup.enter.native="handleQuery" />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="用户openid" prop="openid">
          <el-input v-model="queryParams.openid" placeholder="请输入用户openid" clearable size="small" @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="用户unionid" prop="unionid">
          <el-input v-model="queryParams.unionid" placeholder="请输入用户unionid" clearable size="small" @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item style="margin-left: 30px;">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          <el-button style="margin-left: 30px;" type="warning" icon="el-icon-download" size="mini" @click="handleExport"
            v-hasPermi="['StarActivity:User:export']">导出</el-button>
        </el-form-item>
      </el-row>
    </el-form>

    <el-table v-loading="loading" border stripe :data="UserList" :header-cell-style="{'text-align':'center'}">
      <!-- <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="id" /> -->
      <!-- <el-table-column label="用户头像" align="center" prop="headimgurl" /> -->
      <el-table-column label="报名活动" align="center" prop="fromActivityId" :formatter="fromActivityIdFormat"
        :show-overflow-tooltip="true" width="180"/>
      <el-table-column label="中奖活动" align="center" prop="awardActivityId" :formatter="awardActivityIdFormat"
        :show-overflow-tooltip="true" width="180"/>
      <el-table-column label="用户头像" prop="headimgurl" width="120">
        <template slot-scope="scope">
          <div v-if="scope.row.headimgurl" class="avater-box">
            <img v-if="scope.row.headimgurl" :src="scope.row.headimgurl" alt="" style="width: 70px;height: 70px;border-radius: 50%;">
          </div>
        </template>
      </el-table-column>
      <el-table-column label="用户昵称" align="center" prop="nickname" />
      <el-table-column label="用户openid" align="left" prop="openid" width="220"/>
      <el-table-column label="用户unionid" align="left" prop="unionid" width="220"/>
      <el-table-column label="客户名称" align="center" prop="cName" :show-overflow-tooltip="true" />
      <el-table-column label="客户电话" align="center" prop="cPhone" width="220" :show-overflow-tooltip="true" />
      <el-table-column label="总成交金额" align="center" prop="salesAmount" />
      <el-table-column label="操作" width="100" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="!scope.row.awardActivityId" size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['StarActivity:User:edit']">设置中奖</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['StarActivity:User:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['StarActivity:User:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改星选官用户管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body center :before-close="cancel"
      :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- 活动列表下来选择 中奖活动，填活动id即可 -->
        <el-form-item label="中奖活动" prop="awardActivityId">
          <el-select v-model="form.awardActivityId" placeholder="请选择中奖活动" style="width:100%;">
            <el-option
              v-for="activity in activities"
              :key="activity.id"
              :label="activity.name"
              :value="activity.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户昵称" prop="nickname">
          <el-input v-model="form.nickname" placeholder="请输入用户昵称" />
        </el-form-item>
        <!-- 上传头像控件 -->
        <el-form-item label="用户头像" prop="headimgurl">
          <el-upload :action="imageUploader.action" :headers="imageUploader.headers" :on-success="setFormImageUrl" list-type="picture-card" :file-list="imageUploader.fileList">
            <i slot="default" class="el-icon-plus"></i>
            <div slot="file" slot-scope="{file}" class="my-upload-headimgurl">
              <img class="el-upload-list__item-thumbnail" :src="file.url" alt="" style="width: 100px;height: 100px;border-radius: 50%;">
              <span class="el-upload-list__item-actions">
            <span class="el-upload-list__item-preview" @click="setFormImagePreview(file)">
              <i class="el-icon-zoom-in"></i>
            </span>
            <span class="el-upload-list__item-delete" @click="removeFormImageUrl(file)">
              <i class="el-icon-delete"></i>
            </span>
          </span>
            </div>
          </el-upload>
          <el-dialog :visible.sync="imageUploader.isShowPreview">
            <img width="100%" :src="imageUploader.previewUrl" alt="">
          </el-dialog>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listUser,
    getUser,
    delUser,
    addUser,
    updateUser,
    exportUser
  } from "@/api/StarActivity/User";
  import {
    allListStarActivity
  } from "@/api/StarActivity/StarActivity";

  export default {
    name: "User",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 星选官用户管理表格数据
        UserList: [],
        activities: [], // 活动列表
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          openid: undefined,
          unionid: undefined,
          nickname: undefined,
          fromActivityId: undefined,
          awardActivityId: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {},
        imageUploader:{
          action: process.env.VUE_APP_BASE_API+"/common/upload/news",
          headers: {
            Authorization: 'Bearer ' + this.$store.state.user.token,
            lesseeId: this.$store.state.user.lesseeid
          },
          fileList:[],
          isShowPreview:false,
          previewUrl:""
        }
      };
    },
    created() {
      var that = this;
      that.getActivities(function() {
        that.getList();
      })
    },
    methods: {
      setFormImageUrl(response, file, fileList){
        this.imageUploader.fileList = [file]
        this.form.headimgurl = response.url
      },
      setFormImagePreview(file){
        this.imageUploader.isShowPreview = !this.imageUploader.isShowPreview
        this.imageUploader.previewUrl = file.url
      },
      removeFormImageUrl(file){
        this.imageUploader.previewUrl = ""
        let index = this.imageUploader.fileList.findIndex(obj => obj == file)
        if(index>-1){
          this.imageUploader.fileList.splice(index,1)
        }
      },
      fromActivityIdFormat(row, column) {
        const that = this;
        let activityName = '';
        if (row.fromActivityId && that.activities.length > 0) {
          for (var activity of that.activities) {
            if (activity.id === row.fromActivityId) {
              activityName = activity.name;
              break;
            }
          }
        }
        return activityName;
      },
      awardActivityIdFormat(row, column) {
        const that = this;
        let activityName = '';
        if (row.awardActivityId && that.activities.length > 0) {
          for (var activity of that.activities) {
            if (activity.id === row.awardActivityId) {
              activityName = activity.name;
              break;
            }
          }
        }
        return activityName;
      },
      // 获取活动列表
      getActivities(callback) {
        var that = this;
        this.loading = true;
        allListStarActivity({}).then(response => {
          console.log('获取活动列表');
          console.log(response);
          this.loading = false;
          if (response.code === 200) {
            that.activities = response.data || [];
          }
          if (callback) {
            callback();
          }
        });
      },
      /** 查询星选官用户管理列表 */
      getList() {
        this.loading = true;
        listUser(this.queryParams).then(response => {
          this.UserList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          createdBy: undefined,
          createdTime: undefined,
          updatedBy: undefined,
          updatedTime: undefined,
          isActive: undefined,
          lesseeId: undefined,
          openid: undefined,
          unionid: undefined,
          nickname: undefined,
          headimgurl: undefined,
          fromActivityId: undefined,
          awardActivityId: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      // handleSelectionChange(selection) {
      //   this.ids = selection.map(item => item.id)
      //   this.single = selection.length != 1
      //   this.multiple = !selection.length
      // },
      /** 新增按钮操作 */
      // handleAdd() {
      //   this.reset();
      //   this.open = true;
      //   this.title = "添加星选官用户管理";
      // },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getUser(id).then(response => {
          this.form = response.data;
          if(this.form.headimgurl){
            this.imageUploader.fileList = [{url:this.form.headimgurl}]
          }
          this.open = true;
          this.title = "设置中奖用户";
        });
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              updateUser(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("操作成功");
                  this.open = false;
                  this.getList();
                }
              });
            } else {
              addUser(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("操作成功");
                  this.open = false;
                  this.getList();
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      // handleDelete(row) {
      //   const ids = row.id || this.ids;
      //   this.$confirm('是否确认删除星选官用户管理编号为"' + ids + '"的数据项?', "警告", {
      //     confirmButtonText: "确定",
      //     cancelButtonText: "取消",
      //     type: "warning"
      //   }).then(function() {
      //     return delUser(ids);
      //   }).then(() => {
      //     this.getList();
      //     this.msgSuccess("删除成功");
      //   }).catch(function() {});
      // },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有星选官用户管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportUser(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
      }
    }
  };
</script>
<style>
  .el-tooltip__popper {
    max-width: 800px;
  }

  .avater-box {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .avater-box img {
    width: 100rpx;
    height: 100rpx;
    border-radius: 50%;
  }
  .my-upload-headimgurl{
    width: 148px;
    height: 148px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
</style>
